﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductionManagement.Application.Command.MaterialCommand;
using ProductionManagement.Application.Command.PreProduction;
using ProductionManagement.Application.Command.PreProductionCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Domain.Dtos.PreProductionDtos;
using ProductionManagement.Dtos;
using ProductionManagement.Infrstructre;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProductionManagement.Controllers
{
    /// <summary>
    /// 预生产计划
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PreProductionController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        public PreProductionController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }
        /// <summary>
        /// 添加预生产计划
        /// </summary>
        /// <param name="command">要添加的实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPreProduction(PreProductionCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.addDto.Office_Id = sid;
                command.addDto.AddName = name;
                command.addDto.AddTime = DateTime.Now;
                command.addDto.User_Id = int.Parse(userId);
                result.Code = 1;
                result.Data = await mediatorp.Send(command);
                result.Message = "添加成功";
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 预生产列表
        /// </summary>
        /// <param name="command">查询条件</param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectPreProductionDto>>> PreProductionSelectShow([FromQuery] PreProductionSelectCommand command)
        {
            ApiResult<PageDTO<SelectPreProductionDto>> result = new ApiResult<PageDTO<SelectPreProductionDto>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填预生产计划
        /// </summary>
        /// <param name="cd">要返回的id</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<FanPreProductionDto>> PreProductionFan([FromQuery] PreProductionFanCommand cd)
        {
            ApiResult<FanPreProductionDto> result = new ApiResult<FanPreProductionDto>();
            try
            {
                result.Code = 200;
                result.Message = "反填成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        ///预生产删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> PreProductionDel(PreProductionDelCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.DelName = name;
                cd.DelTime = DateTime.Now;
                result.Code = 200;
                result.Message = "删除成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        ///修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> PreProductionlUpd(PreProductionUpdCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.updDto.UpdName = name;
                cd.updDto.UpdTime = DateTime.Now;
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;





        }
    }
}
